package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageResult;
import com.woniu.mapper.GoodsMapper;
import com.woniu.mapper.ImgsMapper;
import com.woniu.mapper.SortMapper;
import com.woniu.pojo.Goods;
import com.woniu.pojo.Imgs;
import com.woniu.pojo.Sort;
import com.woniu.service.IGoodsService;
import com.woniu.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanMax
 * @since 2022-10-12
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SortMapper sortMapper;
    @Autowired
    private ImgsMapper imgsMapper;

    @Override
    public PageResult search(GoodsVo goodsVo, Integer currentPage, Integer pageSize) {
        Page<Goods> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if(goodsVo!=null){
            if (!StringUtils.isEmpty(goodsVo.getName())){
                wrapper.like("name",goodsVo.getName());
            }
            if (!StringUtils.isEmpty(goodsVo.getId())){
                wrapper.eq("id",goodsVo.getId());
            }
            if (!StringUtils.isEmpty(goodsVo.getIsType())){
                wrapper.eq("is_type",goodsVo.getIsType());
            }
            if (!StringUtils.isEmpty(goodsVo.getSortId())){
                wrapper.eq("sort_id",goodsVo.getSortId());
            }
        }
        Page<Goods> goodsPage = goodsMapper.selectPage(page, wrapper);
        return new PageResult(goodsPage.getTotal(),goodsPage.getRecords());
    }

    @Override
    public Integer deleteById(String id) {
        int i = goodsMapper.deleteById(id);
        QueryWrapper<Imgs> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",id);
        imgsMapper.delete(wrapper);
        return i;
    }

    @Override
    public PageResult selectBySortId(String sortId,Integer currentPage,Integer pageSize) {
        Sort sort = sortMapper.selectById(sortId);
        if (sort.getStatus().equals("1")){
            return null;
        }
        Page<Goods> goodsPage = new Page<>(currentPage,pageSize);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("sort_id",sortId);
        Page<Goods> page = goodsMapper.selectPage(goodsPage, wrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public List<Imgs> getImages(String id) {
        QueryWrapper<Imgs> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",id);
        List<Imgs> imgs = imgsMapper.selectList(wrapper);
        return imgs;
    }
}
